package com.lgh.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lgh.hotel.dao.mapper.GuestMapper;
import com.lgh.hotel.dao.pojo.Guest;
import com.lgh.hotel.service.GuestService;
import com.lgh.hotel.service.TableServer;
import com.lgh.hotel.vo.Result;
import com.lgh.hotel.vo.params.GuestParams;
import io.swagger.models.auth.In;
import org.apache.ibatis.mapping.ResultFlag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service

public class GuestServiceImpl implements GuestService {

    @Autowired
    GuestMapper guestMapper;

    @Autowired
    TableServer tableServer;

    @Override
    public Result findAll() {
        List<Guest> guests = guestMapper.selectList(null);
        return Result.succ(guests);
    }

    @Override
    public Guest findGuestById(Long idCard) {

        LambdaQueryWrapper<Guest> guestLambdaQueryWrapper = new LambdaQueryWrapper<>();
        guestLambdaQueryWrapper.eq(Guest::getIdCard,idCard);
        Guest guest = guestMapper.selectOne(guestLambdaQueryWrapper);

        return guest;
    }

    @Override
    public Result addGuest(Guest guest) {
        return Result.succ(guestMapper.insert(guest));
    }

    @Override
    public Result deleteGuest(Long id) {

        return Result.succ(guestMapper.deleteById(id));
    }

    @Override
    public Result updateGuest(Guest guest) {
        return Result.succ(guestMapper.updateById(guest));
    }

    @Override
    public Result login(GuestParams guestParams) {

        LambdaQueryWrapper<Guest> guestLambdaQueryWrapper = new LambdaQueryWrapper<>();
        guestLambdaQueryWrapper.eq(Guest::getIdCard,guestParams.getCard());
        Guest guest = guestMapper.selectOne(guestLambdaQueryWrapper);

        System.out.println(guest);

        if (guest == null){
            return Result.fail(null);
        }

        System.out.println(guest);
        return Result.succ(guest);
    }

    @Override
    public int findEndTime() {

        LambdaQueryWrapper<Guest> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.le(Guest::getEndTime,System.currentTimeMillis());
        List<Guest> guests = guestMapper.selectList(queryWrapper);
        qingli(guests);
        int delete = guestMapper.delete(queryWrapper);

        return delete;
    }

    @Override
    public Guest findGuestByIdCard(Long idCard) {


        LambdaQueryWrapper<Guest> guestLambdaQueryWrapper = new LambdaQueryWrapper<>();
        guestLambdaQueryWrapper.eq(Guest::getIdCard,idCard);
        Guest guest = guestMapper.selectOne(guestLambdaQueryWrapper);

        return guest;
    }

    @Override
    public void deleteGuestA(Guest guestById) {
        LambdaQueryWrapper<Guest> guestLambdaQueryWrapper = new LambdaQueryWrapper<>();
        guestLambdaQueryWrapper.eq(Guest::getIdCard,guestById.getIdCard());
        guestMapper.delete(guestLambdaQueryWrapper);
    }

    @Override
    public List<Guest> findallIdCrad() {


        List<Guest> guests = guestMapper.selectList(null);
        return guests;

    }

    private void qingli(List<Guest> guests) {
        for (Guest guest : guests) {
            tableServer.findTableByCard(guest.getIdCard());
        }
    }

}
